var api = require('../../../utils/api.js');
var util = require('../../../utils/util.js');
var md = require('../../../utils/md5.js');

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    accountName:'',
    inviteInfo:{},
    uid:'',
    showPage:false
  },

  onLoad: function(options) {
    var that=this;
    if (options.q) {
      var page = decodeURIComponent(options.q);
      var strs = page.split("?"); //字符分割 
      strs = strs[1];
      var params = strs.split('&');
      var uid = params[1];
      that.setData({
        uid: uid.split("=")[1],
      })
    } else {
      that.setData({
        uid: options.uid,
      })
    }
    wx.showLoading({
      title: '加载中...',
    })
  },

  onShow:function(){
    var that=this;
    setTimeout(function () {
      that.joinForeman()
    }, 500)
  },

  //加入店员
  joinForeman: function() {
    var that = this;
    api.POST({
      params: {
        parent_uid:that.data.uid
      },
      url: app.globalData.base_url +'Subaccount/checkStatus',
      success: function(res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.code) {
          util.showError(res.data.message);
        }else{
          that.setData({
            showPage:true,
            inviteInfo: res.data.data,
          })
        }
      },
      fail: function(fail) {
        wx.hideLoading();
        util.showError("网络错误");
      }
    })
  },

  //输入名字
  bindNameInput: function(e) {
    this.setData({
      accountName: e.detail.value
    })
  },

  //输入电话
  bindMobileInput: function(e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  // 提交
  confirmClick: function(e) {
    var that = this;
    if (util.isEmpty(that.data.accountName)) {
      util.showError('请输入名字');
      return;
    }
    var ismobile = util.validateMobile(that.data.mobile);
    if (!ismobile) {
      util.showError('请输入正确的手机号');
      return;
    }

    wx.showLoading({
      title: '提交中...',
    })
    var params = new Object()
    params.parent_uid = that.data.uid;
    params.username = that.data.accountName;
    params.mobile = that.data.mobile;

    api.POST({
      params: params,
      url: app.globalData.base_url +'subAccount/submit',
      success: function(res) {
        wx.hideLoading();
        if (res.data.code) {
          util.showError(res.data.message);
        } else {
          that.onShow();
        }
      },
      fail: function(fail) {
        wx.hideLoading();
        util.showError("网络错误");
      }
    })
  },

  //前往我的小店
  myShop:function(){
    var inviteInfo = this.data.inviteInfo;
    var inn_id = inviteInfo.inn_id;
    wx.redirectTo({
      url: '/agent/pages/shop/shop?inn_id=' + inn_id,
    })
  }
})